package functions.mams_rest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.LoggerFactory;

import com.beans.CityBean;
import com.beans.CompanyBean;
import com.beans.Currency;
import com.beans.GroupTypeBean;
import com.beans.ORGBean;
import com.beans.PayWayBean;
import com.beans.ProjectBean;
import com.beans.SubCategory;
import com.beans.VEHICLE;
import com.crc.mam.BaseCase;
import coms.AutoException;
import coms.Constants;
import coms.JSONUtil;
import frame.HttpHelper;

/**
 * 
 * @author CHENHAILIAN6
 *
 */
public class Sys extends BaseCase{
	private org.slf4j.Logger logger = LoggerFactory.getLogger(Sys.class);
	VEHICLE payType;
    public VEHICLE getPayType() {
		return payType;
	}
	public void setPayType(VEHICLE payType) {
		this.payType = payType;
	}
	String result;
    public VEHICLE getCapitalPlan() {
		return capitalPlan;
	}
	public void setCapitalPlan(VEHICLE capitalPlan) {
		this.capitalPlan = capitalPlan;
	}
	VEHICLE paymentMode;
	public VEHICLE getPaymentMode() {
		return paymentMode;
	}
	public void setPaymentMode(VEHICLE paymentMode) {
		this.paymentMode = paymentMode;
	}
	VEHICLE capitalPlan;
	ArrayList<Currency> currencies;
	ArrayList<CityBean> cities;
	ArrayList<VEHICLE> vehicles;
	ArrayList<VEHICLE> yesNoList;
/*	ArrayList<ORG> orgs;*/

	/**
	 * vehicle_level
	 */
	ArrayList<VEHICLE> vehicleLevels;
	public VEHICLE getSelectLevel() {
		return selectLevel;
	}
	public void setSelectLevel(VEHICLE selectLevel) {
		this.selectLevel = selectLevel;
	}
	public VEHICLE getSelectVehicle() {
		return selectVehicle;
	}
	public void setSelectVehicle(VEHICLE selectVehicle) {
		this.selectVehicle = selectVehicle;
	}



	VEHICLE selectLevel = null;
	VEHICLE selectVehicle = null;

	/**
	 * projectList param ouCode
	 *//*
	ArrayList<Project> projectList;
	Project selectProject = null;*/

	/**
	 * 通过paramCode查询值集
	 * @param paramCode
	 * @return  
	 */
	public JSONArray getValue(String paramCode) {
	  String value = null;
	  JSONArray dataResult = null;
	  String url = Constants.VALUE_SET_lIST_URL+paramCode;
	  try {
        result = HttpHelper.get(null, url);
        status = HttpHelper.isOk;
        if(status) {
          logger.info("getValue --- " + result);
          try {
              JSONObject datas = new JSONObject(result);
              dataResult = datas.getJSONArray("dataResult");
          }catch (JSONException e) {
            throw new AutoException("返回结果不正确");
          }
        }else {
          remark = "服务器返回错误";
          throw new AutoException("服务器返回错误");
        }
    } catch (Exception e) {
      throw new AutoException("服务器返回错误");
    }
	  return dataResult;
	}

	
	/**
	 * 查询货币类型
	 * currencyUrl="/mams-rest/sys/valueset/item/list/currency"
	 */	
	public void currency() {
		logger.info("查询货币类型 ----------------- start ------------");
		String url = Constants.CURRENCY_URL;
		try {
			result = HttpHelper.get(null, url);
			logger.info("currency --- " + result);
			try {
				JSONObject datas = new JSONObject(result);
				JSONArray dataResult = datas.getJSONArray("dataResult");
				currencies = new ArrayList<Currency>();
				for (int i = 0; i < dataResult.length(); i++) {
					JSONObject json = (JSONObject) dataResult.get(i);
					try {
						String vsiId = json.getString("vsiId");
						String vsiKey = json.getString("vsiKey");
						String vsiParentKey = json.getString("vsiParentKey");
						String vsiValue = json.getString("vsiValue");
						Currency currency = new Currency(vsiId, vsiKey, vsiParentKey, vsiValue);
						currencies.add(currency);
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		logger.info("查询货币类型 ----------------- end ------------");
	}
	
	/**
	 * 查询交通出行类型
	 * VEHICLEUUrl="/mams-rest/sys/valueset/item/list/VEHICLE"
	 */
	public void vehicle() {
		logger.info("查询交通出行类型 ----------------- start ------------");
		String url = Constants.VEHICLE_Url;
		try {
			result = HttpHelper.get(null, url);
			logger.info("vehicle --- " + result);
			try {
				JSONObject datas = new JSONObject(result);
				JSONArray dataResult = datas.getJSONArray("dataResult");
				vehicles = new ArrayList<VEHICLE>();
				for (int i = 0; i < dataResult.length(); i++) {
					JSONObject json = (JSONObject) dataResult.get(i);
					try {
						String vsiId = json.getString("vsiId");
						String vsiKey = json.getString("vsiKey");
						String vsiParentKey = json.getString("vsiParentKey");
						String vsiValue = json.getString("vsiValue");
						VEHICLE vehicle = new VEHICLE(vsiId, vsiKey, vsiParentKey, vsiValue);
						vehicles.add(vehicle);
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int size = vehicles.size();
		if (size > 0) {
			Random dom = new Random(size);
			int i = dom.nextInt(size - 1);
			selectVehicle = vehicles.get(i);
			logger.info("selectVehicle ---" + selectVehicle.toString());
		}
		logger.info("查询交通出行类型 ----------------- end ------------");
	}
	
	
	
	/**
	 * 交通类别，一等二等座
	 * 
	 * VEHICLE_LEVELUrl="/mams-rest/sys/valueset/item/list/VEHICLE_LEVEL"
	 */
	public void vehicleLevel() {
		logger.info("交通类别，一等二等座 -----------------start ------------");
		String level = "";
		if (selectVehicle == null) {
			level = "1";
		}
		if (selectVehicle.getVsiKey() == null) {
			level = "1";
		} else if (selectVehicle.getVsiKey() == "") {
			level = "1";
		} else {
			level = selectVehicle.getVsiKey();
		}
		String url =  Constants.VEHICLE_LEVEL_Url;
		url = url + "/" + level;
		// travel_level = new TravelStandard();
		try {
			result = HttpHelper.get(null, url);
			logger.info("vehicle_level --- " + result);
			try {
				JSONObject object = JSONUtil.toJSON(result);
				vehicleLevels = new ArrayList<VEHICLE>();
				JSONArray array = object.getJSONArray("dataResult");
				if (array.length() > 0) {
					for (int i = 0; i < array.length(); i++) {
						JSONObject vehicleObject = (JSONObject) array.get(i);
						String vsiId = vehicleObject.getString("vsiId");
						String vsiKey = vehicleObject.getString("vsiKey");
						String vsiParentKey = vehicleObject.getString("vsiParentKey");
						String vsiValue = vehicleObject.getString("vsiValue");
						VEHICLE cle = new VEHICLE(vsiId, vsiKey, vsiParentKey, vsiValue);
						vehicleLevels.add(cle);
					}
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		int size = vehicleLevels.size();
		if (size > 0) {
			Random dom = new Random(size);
			int i = dom.nextInt(size - 1);
			selectLevel = vehicleLevels.get(i);
			//System.out.println("selectLevel---" + selectLevel.toString());
		}
		logger.info("交通类别，一等二等座 -----------------end ------------");
	}
	
	/**
	 * 还款计划查询
	 * 
	 */
	public void capitalPlan() {
		logger.info(" 还款计划查询 ------- start -------------------");
		String url =Constants.CAPITAL_PLAN_URL;
		try {
			 result = HttpHelper.get(null, url);
			 logger.info("capitalPlan --- " + result);
			 try {
				 JSONObject object = new JSONObject(result);
				 JSONArray array = object.getJSONArray("dataResult");
				 if(array.length() > 0 ) {
					 ArrayList<VEHICLE> capitalPlans = new ArrayList<VEHICLE>();
					 for(int i=0;i<array.length();i++) {
						 JSONObject vel = array.getJSONObject(i);
						 String vsiKey = "";
						 try {
							 vsiKey = (String) vel.get("vsiKey");
						 }catch(Exception e) {
							 
						 }
						 String vsiId = "";
						 try {
							 vsiId = (String) vel.get("vsiId");
						 }catch(Exception e) {
							 
						 }
						 String vsiValue = "";
						 try {
							 vsiValue = (String) vel.get("vsiValue");
						 }catch(Exception e) {
							 
						 }
						 String vsiParentKey = "";
						 try {
							 vsiParentKey = (String) vel.get("vsiParentKey");
						 }catch(Exception e) {
							 
						 }
						 VEHICLE plan = new VEHICLE(vsiId, vsiKey, vsiParentKey, vsiValue);
						 capitalPlans.add(plan);
					 }
					 
					 if(capitalPlans.size()>0) {
						 int size = capitalPlans.size();
						 Random dom = new Random(size);
						 int selectedId = dom.nextInt(size-1);
						 capitalPlan = capitalPlans.get(selectedId);
					 }
				 }
			 }catch(Exception e) {
				 
			 }
		}catch(Exception e) {
			
		}
		logger.info(" 还款计划查询 ------- end -------------------");
	}
	
	/**
	 * 查询付款方式-- 此处为立即还款
	 */
	public void paymentMethod() {
		logger.info(" 查询付款方式 ------- start -------------------");
		String url =Constants.PAYMENT_MODE_URL;
		try {
			 result = HttpHelper.get(null, url);
			 logger.info("paymentMethod --- " + result);
			 try {
				 JSONObject object = new JSONObject(result);
				 JSONArray array = object.getJSONArray("dataResult");
				 if(array.length() > 0 ) {
					 ArrayList<VEHICLE> capitalPlans = new ArrayList<VEHICLE>();
					 for(int i=0;i<array.length();i++) {
						 JSONObject vel = array.getJSONObject(i);
						 String vsiKey = "";
						 try {
							 vsiKey = (String) vel.get("vsiKey");
						 }catch(Exception e) {
							 
						 }
						 String vsiId = "";
						 try {
							 vsiId = (String) vel.get("vsiId");
						 }catch(Exception e) {
							 
						 }
						 String vsiValue = "";
						 try {
							 vsiValue = (String) vel.get("vsiValue");
						 }catch(Exception e) {
							 
						 }
						 String vsiParentKey = "";
						 try {
							 vsiParentKey = (String) vel.get("vsiParentKey");
						 }catch(Exception e) {
							 
						 }
						 VEHICLE plan = new VEHICLE(vsiId, vsiKey, vsiParentKey, vsiValue);
						 capitalPlans.add(plan);
					 }
					 
					 if(capitalPlans.size()>0) {
						 int size = capitalPlans.size();
						 Random dom = new Random(size);
						 int selectedId = dom.nextInt(size-1);
						 paymentMode = capitalPlans.get(selectedId);
					 }
				 }
			 }catch(Exception e) {
				 
			 }
		}catch(Exception e) {
			
		}
		logger.info(" 查询付款方式 ------- end -------------------");
	}

	
	/**
	 * 查询付款类型
	 */
	public void payType() {
		logger.info(" 查询付款类型------- start -------------------");
		String url =Constants.PAY_TYPE_URL;
		try {
			 result = HttpHelper.get(null, url);
			 logger.info("payType --- " + result);
			 try {
				 JSONObject object = new JSONObject(result);
				 JSONArray array = object.getJSONArray("dataResult");
				 if(array.length() > 0 ) {
					 ArrayList<VEHICLE> capitalPlans = new ArrayList<VEHICLE>();
					 for(int i=0;i<array.length();i++) {
						 JSONObject vel = array.getJSONObject(i);
						 String vsiKey = "";
						 try {
							 vsiKey = (String) vel.get("vsiKey");
						 }catch(Exception e) {
							 
						 }
						 String vsiId = "";
						 try {
							 vsiId = (String) vel.get("vsiId");
						 }catch(Exception e) {
							 
						 }
						 String vsiValue = "";
						 try {
							 vsiValue = (String) vel.get("vsiValue");
						 }catch(Exception e) {
							 
						 }
						 String vsiParentKey = "";
						 try {
							 vsiParentKey = (String) vel.get("vsiParentKey");
						 }catch(Exception e) {
							 
						 }
						 VEHICLE plan = new VEHICLE(vsiId, vsiKey, vsiParentKey, vsiValue);
						 capitalPlans.add(plan);
					 }
					 
					 if(capitalPlans.size()>0) {
						 int size = capitalPlans.size();
						 Random dom = new Random(size);
						 int selectedId = dom.nextInt(size-1);
						 payType = capitalPlans.get(selectedId);
					 }
				 }
			 }catch(Exception e) {
				 
			 }
		}catch(Exception e) {
			
		}
		logger.info(" 查询付款类型------- end -------------------");
	}
	
	/**
	 * 选择审批意见为同意
	 */
	public void yesNo() {
		logger.info("选择审批意见为同意 ------------------- start---------------------");
		String url =Constants.YES_NO_URL;
    	try {
			result = HttpHelper.get(null, url);
			logger.info("yes_no --- " + result);
			try {
				JSONObject object = new JSONObject(result);
					JSONArray array = object.getJSONArray("dataResult");
					yesNoList = new ArrayList<VEHICLE>();
				for(int i=0;i<array.length();i++) {
				    JSONObject detail = array.getJSONObject(i);
				    String vsiKey = "";
				    try{
				    	vsiKey = (String) detail.get("vsiKey");
				    }catch(Exception e) {
			    		//e.printStackTrace();
			    	}
				    String vsiId = "";
				    try{
				    	vsiId = (String) detail.get("vsiId");
				    }catch(Exception e) {
			    		//e.printStackTrace();
			    	}
				    String vsiValue = "";
				    try{
				    	vsiValue = (String) detail.get("vsiValue");
				    }catch(Exception e) {
			    		//e.printStackTrace();
			    	}
				    String vsiParentKey = "";
				    try{
				    	vsiParentKey = (String) detail.get("vsiParentKey");
				    }catch(Exception e) {
			    		//e.printStackTrace();
			    	}
				    VEHICLE veh = new VEHICLE(vsiId, vsiKey, vsiParentKey, vsiValue);
				   // System.out.println(veh.toString());
				    yesNoList.add(veh);				    
				}
			}catch(Exception e) {
				e.printStackTrace();
			}
    	}catch(Exception e) {
    		
    	}
    	logger.info("选择审批意见为同意 ------------------- end---------------------");
	}
	
	/**
	 * 
	 * @param paramMap
	 * /mams-rest/sys/valueset/item/list/payWay/3
	 */
	public PayWayBean payWay(Map<Object, Object> paramMap) {
		logger.info("查询付款方式------------------- start---------------------");
		 PayWayBean payWayBean = null;
		String payWayId = "1";
		if(paramMap == null) {
			
		}else {
			payWayId = (String )paramMap.get("payWay");
			if(payWayId == null) {
				payWayId = "1";
			}
			
			
		}	
		String url =Constants.PAY_WAY_URL+"/"+payWayId;
    	try {
			result = HttpHelper.get(null, url);
			if(HttpHelper.isOk) {
				logger.info("yes_no --- " + result);
				try {
					JSONObject object = new JSONObject(result);
						JSONArray array = object.getJSONArray("dataResult");
						JSONObject detail = array.getJSONObject(0);
						String vsiKey = "";
						try{
							vsiKey = (String) detail.get("vsiKey");
						}catch(Exception e) {
							//e.printStackTrace();
						}
						String vsiId = "";
						try{
							vsiId = (String) detail.get("vsiId");
						}catch(Exception e) {
							//e.printStackTrace();
						}
						String vsiValue = "";
						try{
							vsiValue = (String) detail.get("vsiValue");
						}catch(Exception e) {
							//e.printStackTrace();
						}
						String vsiParentKey = "";
						try{
							vsiParentKey = (String) detail.get("vsiParentKey");
						}catch(Exception e) {
							//e.printStackTrace();
						}
						payWayBean = new PayWayBean(vsiId, vsiKey, vsiParentKey, vsiValue);
				   // System.out.println(veh.toString());		
				}catch(Exception e) {
					e.printStackTrace();
				}
			}
    	}catch(Exception e) {
    		
    	}
		return payWayBean;
    
	}
	
	/**
	 * 会计池审核类型：核算会计，复核会计
	 * @return
	 */
	public List<GroupTypeBean>  accountGroupType() {
	  ArrayList<GroupTypeBean> groupTypeList = null ;
      String url = Constants.ACCOUNT_GROUP_TYPE_URL;
      try {
        result = HttpHelper.get(null, url);
        if(HttpHelper.isOk) {
            logger.info("ACCOUNT_GROUP_TYPE --- " + result);
            try {
                JSONObject object = new JSONObject(result);
                groupTypeList =  new ArrayList<GroupTypeBean>();
                    JSONArray array = object.getJSONArray("dataResult");
                    for(int i= 0;i<array.length();i++) {
                      JSONObject detail = array.getJSONObject(i);
                      String vsiKey = "";
                      try{
                          vsiKey = (String) detail.get("vsiKey");
                      }catch(Exception e) {
                        //e.printStackTrace();
                      }
                      String vsiId = "";
                      try{
                        vsiId = (String) detail.get("vsiId");
                      }catch(Exception e) {
                        //e.printStackTrace();
                      }
                      String vsiValue = "";
                      try{
                        vsiValue = (String) detail.get("vsiValue");
                      }catch(Exception e) {
                        //e.printStackTrace();
                      }
                      String vsiParentKey = "";
                      try{
                        vsiParentKey = (String) detail.get("vsiParentKey");
                      }catch(Exception e) {
                        //e.printStackTrace();
                      }
                      GroupTypeBean groupBean = new GroupTypeBean(vsiId, vsiKey, vsiParentKey, vsiValue);
                      groupTypeList.add(groupBean);
                       // System.out.println(veh.toString());       
                    }
            
            }catch(Exception e) {
        
            }
        }
      }catch(Exception e) {
        logger.error("接口错误");
      }
	  return groupTypeList;
	}
      
}
